#include "XSystem/XCore.h"

// Constuct the textfield with the pre-defined data.
XTextField::XTextField(XResourcesManager *data, int positionX, int positionY, int width) : XWidget(data, width, 12, positionX, positionY)
{
	x_type = XG_TYP_TEXTFIELD;

	x_background = sf::Shape::Rectangle(positionX, positionY, (positionX + x_width), (positionY + 12), sf::Color(255, 255, 255), 1, sf::Color(0, 0, 0));

	x_blinker = sf::Shape::Line((positionX + 4), (positionY + 2), (positionX + 4), (positionY + 10), 1, sf::Color(0, 0, 0));

	x_string.SetFont(x_data->GetFont());
	x_string.SetSize(8);
	x_string.SetPosition(x_positionX+2, x_positionY+2);
	x_string.SetColor(sf::Color(0, 0, 0));

	x_isPassword = false;
	x_text = "";

	x_blinkerTimer = 0;
	x_blinkerState = false;
}

// Draws the textfield.
void XTextField::Draw()
{
	// Blinker
	x_blinkerTimer++;
	if (x_blinkerTimer > 20)
	{
		x_blinkerState = ((x_blinkerState + 1) % 2); 
		x_blinkerTimer = 0;
	}

	// Draw
	x_app->Draw(x_background);
	x_app->Draw(x_string);

	if (x_blinkerState && x_focus == XG_FOC_YES)
		x_app->Draw(x_blinker);
}

// Check the state of the textfield.
void XTextField::CheckState(const sf::Input &input)
{
	XWidget::CheckState(input);

	switch (x_state)
	{
		case XG_ST_MOUSE_ENTER:
			x_background.SetColor(sf::Color(172, 172, 172));
			break;

		case XG_ST_MOUSE_EXIT:
			x_background.SetColor(sf::Color(255, 255, 255));
			break;

		case XG_ST_LBUTTON_DOWN:
			break;

		case XG_ST_LBUTTON_RELEASED:
			break;	
	}
}

// Check the events, to see if the user is typing.
// \return the state
void XTextField::CheckEvent(const sf::Event &Event)
{
	if (x_focus == XG_FOC_YES)
	{
		// Typing text
        if (Event.Type == sf::Event::TextEntered)
        {
			sf::FloatRect stringRect = x_string.GetRect();
			int stringWidth = stringRect.GetWidth();

            if (stringWidth < (x_width - 8))
            {
                if (Event.Text.Unicode > 31 && Event.Text.Unicode < 128)
                {
                    x_text += static_cast<char>(Event.Text.Unicode);

                    x_string.SetText(x_text);
					if (x_isPassword)
					{
						std::string tmp = "";

						for (int i=0; i<x_text.size(); i++)
							tmp += "*";

						x_string.SetText(tmp);
					}

					sf::FloatRect stringRect = x_string.GetRect();
					int stringWidth = stringRect.GetWidth();

					x_blinker.SetX(stringWidth);
                }
            }
        }

        // Pressing the backspace
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Back))
        {
            int tmp = x_text.size()-1;
            if (tmp>-1)
                x_text.resize(tmp, '.');

            x_string.SetText(x_text);
			if (x_isPassword)
			{
				std::string tmp = "";

				for (int i=0; i<x_text.size(); i++)
					tmp += "*";

				x_string.SetText(tmp);
			}

			sf::FloatRect stringRect = x_string.GetRect();
			int stringWidth = stringRect.GetWidth();

			x_blinker.SetX(stringWidth);
        }
	}
}

// Set the text.
void XTextField::SetText(std::string text)
{
	x_text = text;

	if (x_isPassword)
	{
		std::stringstream ss;
		for (int i=0; i<int(text.size()); i++)
			ss << "*";

		x_string.SetText(ss.str());

	} else {

		x_string.SetText(text);
	}

	sf::FloatRect stringRect = x_string.GetRect();
	int stringWidth = stringRect.GetWidth();

	x_blinker.SetX(stringWidth);
}

// \return the text
std::string XTextField::GetText()
{
	return x_text;
}

// Set if the text is a password or not.
void XTextField::SetPassword(bool isPassword)
{
	x_isPassword = isPassword;
}

// Set the focus state
void XTextField::SetFocus(bool focus)
{
	XWidget::SetFocus(focus);
}
